package com.game.classes
{
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.SharedObject;
	public class ConnectionManager extends EventDispatcher
	{
		private var connection:NetConnection;
    	private var netstream:NetStream;
    	private var so:SharedObject;
    	
    	[Bindable]
    	public var msgReceived : String;
		
		public function ConnectionManager() 
		{
			
		}

		public function connect(serverUrl:String):void{
			connection= new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, netRecordHandler);
           	connection.connect( "rtmp://localhost/firstapp" , false );
			so = SharedObject.getRemote("message", connection.uri, false);
    		so.client = this;
    		so.connect(connection);
            	
		}
		public function sendTestMessage():void{
 		   connection.call("sendMessage", null, "Enviando HOLA");
		}
		
		private function netRecordHandler(event:NetStatusEvent):void{
			switch (event.info.code) {
                case "NetConnection.Connect.Success":
					trace("Connected...");	
					netstream = new NetStream(connection);
					break;
				default:
					trace("did not connect");
					break;
			}	
		}
		public function receiveMessage(mesg:String):void{
    	 	msgReceived = new String(mesg);
			dispatchEvent(new GameEvents(GameEvents.SN_NEW_ANGLE)); 
		}
	}
}